package app.fugema.shop.main.center.view;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.base.common.app.LoginUtils;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.UIUtils;
import com.base.common.view.base.BaseFragment;
import com.base.common.view.base.OnGetDataListener;
import com.base.common.viewmodel.BaseViewObserver;
import com.jeremyliao.liveeventbus.LiveEventBus;

import app.fugema.shop.R;
import app.fugema.shop.constants.AppConstants;
import app.fugema.shop.databinding.OrderListFmBinding;
import app.fugema.shop.main.center.adapter.GitfOrderListAdapter;
import app.fugema.shop.main.center.model.GiftOrdersResult;
import app.fugema.shop.main.center.vm.JiFenOrderVM;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;

public class GiftOrderListFragment extends BaseFragment<OrderListFmBinding, JiFenOrderVM> implements OnGetDataListener {

    public static GiftOrderListFragment getInstance(int type) {
        Bundle args = new Bundle();
        args.putInt("type", type);
        GiftOrderListFragment fragment = new GiftOrderListFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected OrderListFmBinding initDataBinding(LayoutInflater inflater, ViewGroup container) {
        return DataBindingUtil.inflate(inflater, R.layout.order_list_fm, container, false);
    }

    private int type; //订单状态 0全部 2:待发货   3待收货 5已完成

    private GitfOrderListAdapter orderListAdapter;

    private int ps = 9;


    @Override
    protected void initNotificationReceipt() {
        super.initNotificationReceipt();

        //确认收货成功
        LiveEventBus.get(AppConstants.EventKey.GIFT_ORDER_RECEIPT, String.class).observe(this, new Observer<String>() {
            @Override
            public void onChanged(String o) {
                if (type == 5)
                {
                    //已完成刷新
                    onGetData(1);
                }
                else if (type == 0 || type == 3 )
                {
                    for (int i = 0; i < orderListAdapter.getChildCount(); i++)
                    {
                        if (orderListAdapter.getListBean(i).getId() == Long.valueOf(o))
                        {
                            int pn = i / ps + 1;
                            onGetData(pn);
                            break;
                        }
                    }
                }
            }
        });
    }

    @Override
    public void initView()
    {
        super.initView();
        orderListAdapter = new GitfOrderListAdapter(this, viewModel);
        binding.rvRecyclerView.setAdapter(orderListAdapter);

        viewUtils.setRefreshStateLayout(binding.rvRecyclerView, this, this);
    }

    @Override
    public void initData()
    {
        super.initData();
        type = getArguments().getInt("type");
        onGetData(1);
    }


    @Override
    public void onGetData(int pn)
    {
        String userKey = LoginUtils.getToken();
        MutableLiveData<LiveDataWrapper<GiftOrdersResult>> liveData = viewModel.getMyGiftOrders(type, pn,userKey);
        liveData.observeForever(new BaseViewObserver<LiveDataWrapper<GiftOrdersResult>>(viewUtils.getStateLayout()) {
            @Override
            protected void onSuccess(LiveDataWrapper<GiftOrdersResult> data)
            {
                viewUtils.setDataListRefreshLayout(orderListAdapter, pn, ps,data.data.getData().getRows());
            }

            @Override
            public boolean isEmptyData(LiveDataWrapper<GiftOrdersResult> data)
            {
                return pn == 1 && UIUtils.isNotEmpty(data.data.getData())&&UIUtils.isEmpty(data.data.getData().getRows());
            }

            @Override
            public boolean isRefreshLayoutData()
            {
                return true;
            }

            @Override
            protected void onComplete()
            {
                super.onComplete();

                liveData.removeObserver(this);
            }
        });
    }
}
